package com.oyjd;

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;


public class DownMusic {
	public static String url1 = "http://music.baidu.com/?from=new_mp3";
	public static String reg1 = "<li\\s*class=\"clearfix.*?\">.*?<a.*?sid=\"(.*?)\".*?>(.*?)</a>.*?<span class=\"author_list\" title=\"(.*?)\">.*?</li>";

	public static String url2 = "http://music.baidu.com/song/{0}/download";
	public static String reg2 = "<div class=\"operation clearfix\">.*?<a href=\"(.*?)\".*?class=\"btn-download\">.*?</div>";

	public static String url3 = "http://music.baidu.com";

	public static String sql = "insert into T_MUSIC (MUSIC_ID, SINGER, DESCRIPTION, FORMAT, NAME, SOU_PATH, SPACE_SIZE, CREATOR, CREATE_TIME, LAST_EDITOR, LAST_UPDATE_TIME, STATUS) values (''{0}'', ''{1}'', ''{2}'', ''mp3'', ''{2}'', ''{3}'', {4}, ''admin'', now(), ''admin'', now(), 0);";
	public static String hot_sql = "insert into T_HOT_SOURCE (DOWN_COUNT, SOU_ID, SOU_TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";
	public static String order_sql = "insert into T_MUSIC_TYPE_ORDER (ORDER_KEY, MUSIC_ID, TYPE, ID) values ({0}, ''{1}'', ''{2}'', ''{3}'');";
	
	public static String souPath = "E:/SL/trunk/music/";
	public static String sqlPath = "E:/SL/trunk/script/music.sql";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		HttpNet hn = new HttpNet();
		String c = new String(hn.getBytesFromNet(url1));
		List<String[]> tmp1 = RegexUtil.getComplex(c, reg1, new int[] { 1, 2, 3 });
		int count = tmp1.size();
		StringBuffer sb = new StringBuffer("use sldb;\r\n\r\n");
		StringBuffer sb1 = new StringBuffer("\r\n#==============================\r\n");
		StringBuffer sb2 = new StringBuffer("\r\n#==============================\r\n");
		List<String> keys = new ArrayList<String>();
		int p = 1;
		for (int i=0;i<tmp1.size();i++) {
			String id = tmp1.get(i)[0];
			String name = tmp1.get(i)[1];
			String author = tmp1.get(i)[2];
			String key = author + "-" + name + "-" + id;
			if (keys.contains(key)) {
				continue;
			} else {
				keys.add(key);
			}
			System.out.println(p++ + "-->" + key);

			String url = MessageFormat.format(url2, id);
			String c2 = new String(hn.getBytesFromNet(url));
			String tmp2 = RegexUtil.getSingle(c2, reg2);

			// down
			String urlDown = url3 + tmp2;
			String iconNewName = FileOP.getNewHashName(key + ".mp3", "sl_music");
			File file = new File(souPath + iconNewName);
			if (file.exists() && file.length() == 0) {
				file.delete();
			}
			if (!file.exists()) {
				hn.downFile(urlDown, null, souPath + iconNewName);
			}

			// sql
			if (file.exists() && file.length() > 0) {
				String souId = FileOP.getUUID();
				String newSql = MessageFormat.format(sql, souId, author, name, iconNewName, Long.toString(file.length()));
				sb.append(newSql).append("\r\n");
				String newHotSql = MessageFormat.format(hot_sql, count--, souId, "TYPE_MUSIC", FileOP.getUUID());
				sb1.append(newHotSql).append("\r\n");
				String typeSql = MessageFormat.format(order_sql, i + 1, souId, "Music_Boutique", FileOP.getUUID());
				sb2.append(typeSql).append("\r\n");
			}

		}
		FileOP.writeFile(sqlPath, sb.toString() + sb1.toString()+ sb2.toString(), "gb2312");
	}
}
